package routine

import (
	"log"
	"os"
	"os/signal"
	"sync"
)

var WaitGroup WaitGroupWrapper

type WaitGroupWrapper struct {
	sync.WaitGroup
}

func (w *WaitGroupWrapper) Wrap(cb func()) {
	w.Add(1)
	go func() {
		defer w.Done()
		cb()
	}()
}

func signalListen() {
	sign := make(chan os.Signal)
	signal.Notify(sign, os.Interrupt)
	<-sign
	Exit()
}

var end = make(chan byte)
var vaild = true

func Exit() {
	if vaild {
		defer func() {
			if err := recover(); err != nil {
				log.Println(err)
			}
		}()
		vaild = false
		close(end)
		log.Println("Stopping.")
	}
}

//等待退出，用select 操作
func WaitExit() <-chan byte {
	return end
}
